var tab;
$(function () {
    initTab();
    initRadio();
    initEvent();
})

function initEvent() {
    $(".rechargeBtn").on("click", function () {
        rechargeClick();

    });
    $(".rechargeCompleted").on("click", function () {
        rechargeCompleted();
    });
    $(".presentBtn").on("click", function () {
        presentBtnClick();
    });
    $(".rechargeCancel").on("click", function () {
        rechargeCancel();
    });
    $(".rechargeReset").on("click", function () {
        $(".rechargeNum").val("");
    });
    $(".presentReset").on("click", function () {
        $(".presentNum").val("");
    });
}

function rechargeCancel() {
    $.ajax({
        type: 'post',
        url: webRoot + "/rechargeRecord/user/deleteRechargeRecord.do",
        cache: false,
        data: {
            id: $(".rechargeId").val(),
        },
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                $(".recharge").show();
                $(".present").hide();
                $(".rechargeNum").val("")
                $(".account-detail").hide()
            } else {
                if (data.msg) {
                    Message.show(data.msg, "error", 2000);
                } else {
                    Message.show("系统异常", "error", 2000);
                }
            }
        },
        error: function () {
            return;
        }
    });
}

function rechargeCompleted() {
    $.ajax({
        type: 'post',
        url: webRoot + "/rechargeRecord/user/saveRechargeRecord.do",
        cache: false,
        data: {
            id: $(".rechargeId").val(),
            state: '审核中'
        },
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                $(".recharge").show();
                $(".present").hide();
                $(".rechargeNum").val("")
                $(".account-detail").hide()
            } else {
                if (data.msg) {
                    Message.show(data.msg, "error", 2000);
                } else {
                    Message.show("系统异常", "error", 2000);
                }

            }

        },
        error: function () {
            return;
        }
    });

}

function rechargeClick() {
    var value = $(".rechargeNum").val();
    if (value == "" || value == undefined || value == null) {
        BUI.Message.Alert('充值金额不能为空！', function () {
        }, 'warning');
        return;
    }
    $.ajax({
        type: 'post',
        url: webRoot + "/rechargeRecord/user/saveRechargeRecord.do",
        cache: false,
        data: {
            amount: value,
            state: '未转账'
        },
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                $(".recharge").hide();
                $(".present").hide();
                $(".money1").html(value)
                $(".remark").html(session.userInfo.id)
                $(".rechargeId").val(data.object.id)
                $(".account-detail").show()
            } else {
                if (data.msg) {
                    Message.show(data.msg, "error", 2000);
                } else {
                    Message.show("系统异常", "error", 2000);
                }

            }

        },
        error: function () {
            return;
        }
    });
}

function initTab() {
    BUI.use('bui/tab', function (Tab) {
        tab = new Tab.Tab({
            render: '#tab',
            elCls: 'nav-tabs',
            autoRender: true,
            children: [
                {text: '账户充值', value: '1'},
                {text: '账户提现', value: '2'}
            ]
        });
        tab.on('selectedchange', function (ev) {
            var item = ev.item;
            tabChange(item)
            $('#log').text(item.get('text') + ' ' + item.get('value'));
        });
        tab.setSelected(tab.getItemAt(0));
    });

}

function tabChange(item) {
    if (item.get('value') == "1") {
        $(".recharge").show();
        $(".present").hide();
        $(".account-detail").hide();
    } else {
        $(".recharge").hide();
        $(".present").show();
        $(".account-detail").hide();
    }

}

function initRadio() {
    $(".bankInfo").empty();
    var html = ""
    for (var i in bankCards) {
        var value=bankCards[i].bankName + " " + bankCards[i].carNo
        html = '<p><input type="radio" value ="'+value+'"name="bankInfo"/>' + value + '</p>'
        $(".bankInfo").append(html);
    }


}

function presentBtnClick() {
    var value = $(".presentNum").val()
    if (value == "" || value == undefined || value == null) {
        BUI.Message.Alert('提现金额不能为空！', function () {
        }, 'warning');
        return;
    }
    var bankInfo = $('input:radio:checked').val();
    if (bankInfo == "" || bankInfo == undefined || bankInfo == null) {
        BUI.Message.Alert('提现银行不能为空！', function () {
        }, 'warning');
        return;
    }
    var bankInfoArr = bankInfo.split(" ");
    $.ajax({
        type: 'post',
        url: webRoot + "/presentRecord/user/savePresentRecord.do",
        cache: false,
        data: {
            amount: value,
            carNo: bankInfoArr[1],
            bankName: bankInfoArr[0],
            state: '未审核'
        },
        dataType: 'json',
        success: function (data) {
            if (data.success) {
                BUI.Message.Alert("申请成功，请等待审核！", 'info');
                $(".presentNum").val("")
                var balance = data.object.balance - data.object.frozeMoney
                $(".balance").html(balance.toFixed(2)+"元")
            } else {
                if (data.msg) {
                    Message.show(data.msg, "error", 2000);
                } else {
                    Message.show("系统异常", "error", 2000);
                }

            }

        },
        error: function () {
            return;
        }
    });
}
